package com.password.desktop.page;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.password.desktop.UI.AbstractMovedWindow;
import com.password.desktop.UI.ImageJLable;
import com.password.desktop.UI.PMLineBorder;
import com.password.desktop.exception.NoLoginException;
import com.password.desktop.service.LoginService;
import com.password.desktop.util.MessageUtil;
import com.password.desktop.util.SysUtil;
import com.password.entity.User;

/**
 * 菜单窗口
 * 
 * @author yzh
 * @date 2020年6月4日
 *
 */
public class MenuWindow extends AbstractMovedWindow {

	LoginService loginService = LoginService.builderLoginService();

	private static final long serialVersionUID = 1L;
	String title = "PM";
	private static final int HEIGHT_WINDOW = 360; // 窗口高度
	private static final int WIDTH_WINDOW = 320; // 窗口宽度
	private static final int HEIGHT_HEAD = 32;
	private static final Dimension DIMENSION_DEFAULT = new Dimension(WIDTH_WINDOW / 4, 80);
	private static final Dimension DIMENSION_DEFAULT_MAX = new Dimension(WIDTH_WINDOW / 4 + 20, 80);

	private JPanel head; // 头部容器
	JLabel logo, minW, closeW;// logo,最小化，关闭

	public MenuWindow() {
//		getHead();
		getContentPane().add(getHead());
		getContentPane().add(getContent());
		initSet();
	}

	public JPanel getHead() {
		User currentUser = loginService.getCurrentUser();
		if (currentUser == null) {
			MessageUtil.showMessage(null, "未登录");
			dispose();
			new LoginWindow();
			throw new NoLoginException("未登录");
		}
		head = new JPanel();
		head.setBackground(new Color(153, 255, 204));
		head.setLayout(null);
		head.setVisible(true);
		head.setBounds(0, 0, WIDTH_WINDOW, HEIGHT_HEAD);
		head.setForeground(Color.WHITE);

		JLabel backgroud = new JLabel(new ImageIcon(getResoure("素材/1.gif")));
		backgroud.setOpaque(false);
		JLabel title = new JLabel("PM " + currentUser.getUserName());
		logo = new JLabel(SysUtil.ICON_LOGO_MIDDLE);
		minW = new JLabel(SysUtil.ICON_MIN);
		closeW = new JLabel(SysUtil.ICON_CLOSE);
		backgroud.setBounds(0, 0, WIDTH_WINDOW, HEIGHT_HEAD);
		logo.setBounds(0, 0, HEIGHT_HEAD, HEIGHT_HEAD);
		title.setBounds(HEIGHT_HEAD + 2, 0, WIDTH_WINDOW - 64, HEIGHT_HEAD);
		title.setFont(SysUtil.FONT_DEFAULT_MIN);
		minW.setBounds(WIDTH_WINDOW - 64 - 4, 0, HEIGHT_HEAD, HEIGHT_HEAD);
		minW.setBackground(new Color(0, 0, 0, 0.3f));
		closeW.setBounds(WIDTH_WINDOW - 32, 0, HEIGHT_HEAD, HEIGHT_HEAD);
		closeW.setBackground(new Color(0, 0, 0, 0.3f));
		// 事件区域
		minW.addMouseListener(this);
		closeW.addMouseListener(this);

		head.add(logo);
		head.add(title);
		head.add(minW);
		head.add(closeW);
		head.add(backgroud);

		return head;
	}

	public JPanel getContent() {
		JPanel panel = new JPanel();
		FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 0, 0);
		panel.setBackground(Color.white);
		panel.setLayout(flowLayout);
		panel.setVisible(true);
		panel.setBounds(0, HEIGHT_HEAD, WIDTH_WINDOW, HEIGHT_WINDOW);
		panel.setForeground(Color.WHITE);

		JLabel item1 = getContentItem("文本加密解密", getResoure("images/functionSelect/textEncrpt.png"));
		item1.setBorder(PMLineBorder.builder());
		item1.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) {
					MenuWindow.this.dispose();
					new TextEncrptWindow();
				}
			}
		});
		panel.add(item1);
		JLabel item2 = getContentItem("文件加密解密", getResoure("images/functionSelect/fileEncrptMin.png"));
		item2.setBorder(PMLineBorder.builder().left(null));
		item2.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) {
					MenuWindow.this.dispose();
					new FileEncrptWindow();
				}
			}
		});
		panel.add(item2);
		JLabel item3 = getContentItem("MD5加密", getResoure("images/functionSelect/MD5.png"));
		item3.setBorder(PMLineBorder.builder().left(null));
		panel.add(item3);
		JLabel item4 = getContentItem("SHA加密", getResoure("images/functionSelect/sha-256.png"));
		item4.setBorder(PMLineBorder.builder().left(null));
		panel.add(item4);

		JLabel item5 = getContentItem("RSA加密解密", getResoure("images/functionSelect/RSA.png"));
		item5.setBorder(PMLineBorder.builder().top(null));
		panel.add(item5);
		JLabel item6 = getContentItem("BASE64", getResoure("images/functionSelect/base64.png"));
		item6.setBorder(PMLineBorder.builder().top(null).left(null));
		panel.add(item6);

		return panel;
	}

	private JLabel getContentItem(String title, URL iconUrl) {
		JLabel item = new JLabel();
		item.setPreferredSize(DIMENSION_DEFAULT);
		item.setBackground(Color.white);
		item.setLayout(null);
		// icon
		JLabel icon = new ImageJLable(iconUrl);
		icon.setBounds(22, 12, 35, 32);
		// text
		JLabel text = new JLabel(title);
		text.setBounds(0, 47, WIDTH_WINDOW / 4, 28);
		text.setForeground(Color.DARK_GRAY);
		text.setFont(SysUtil.FONT_DEFAULT_MIN);
		text.setHorizontalAlignment(SwingConstants.CENTER);

		item.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseEntered(MouseEvent e) {
				icon.setBounds(22, 12, 38, 36);
				text.setBounds(0, 47, WIDTH_WINDOW / 4 + 2, 28 + 2);
				item.setPreferredSize(DIMENSION_DEFAULT_MAX);
				item.setBackground(Color.lightGray);
				item.setOpaque(true);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				icon.setBounds(22, 12, 35, 32);
				text.setBounds(0, 47, WIDTH_WINDOW / 4, 28);
				item.setPreferredSize(DIMENSION_DEFAULT);
				item.setBackground(Color.white);
				item.setOpaque(true);
			}

		});

		item.add(icon);
		item.add(text);
		return item;
	}

	public void initSet() {
		this.setSize(WIDTH_WINDOW, HEIGHT_WINDOW);
		super.initSet();
		setTitle("PM " + loginService.getCurrentUser().getUserName());
		setVisible(true);
	}

	public static void main(String[] args) {
		new MenuWindow();
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.getSource() == minW) {
			setExtendedState(JFrame.ICONIFIED);
		} else if (e.getSource() == closeW) {
			System.exit(0);
		} else if (e.getSource() == this) {
			super.mousePressed(e);
		}

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (e.getSource() == minW) {
			minW.setOpaque(true);
			minW.setBackground(Color.lightGray);
		} else if (e.getSource() == closeW) {
			closeW.setOpaque(true);
			closeW.setBackground(Color.lightGray);
		}

	}

	@Override
	public void mouseExited(MouseEvent e) {
		if (e.getSource() == minW) {
			minW.setOpaque(false);
			minW.setBackground(new Color(0, 0, 0, 0.3f));
		} else if (e.getSource() == closeW) {
			closeW.setOpaque(false);
			closeW.setBackground(new Color(0, 0, 0, 0.3f));
		}

	}

}
